package com.chinacache.maint.webssh.ldap;

import java.util.Hashtable;

import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;

@SuppressWarnings("unchecked")
public class LDAP {

	private Hashtable env = null;

	private static DirContext ctx = null;

	private boolean islogin = false;

	StringBuffer url;

	String host;

	String port;

	String admin;

	String password;

	String baseDN;

	public LDAP(String id, String pwd) {
		try {
			host = UMParas.getPara("hostname");
			port = UMParas.getPara("port");
			baseDN = UMParas.getPara("basedn");
			admin = UMParas.getPara("admin");
			password = UMParas.getPara("pwd");
			url = new StringBuffer("LDAP://");
			url.append(host).append(":").append(port);
			url.append("/").append(baseDN);

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("");
		}
		// pwd="secret";
		env = new Hashtable();

		env.put("java.naming.factory.initial",
				"com.sun.jndi.ldap.LdapCtxFactory");
		env.put("java.naming.provider.url", url.toString());
		env.put(Context.SECURITY_AUTHENTICATION, "simple");

		env.put("java.naming.security.principal", admin);
		env.put("java.naming.security.credentials", password);
		System.out.println("-------------");
	}

	@SuppressWarnings("unused")
	public boolean checkAd() { // admin用户验证
		try {
			System.out.println("-----ddd--------");
			InitialContext iCnt = new InitialContext(env);
			System.out.println("-------eee------");
			islogin = true;
		} catch (AuthenticationException aue) {
			// aue.printStackTrace();
			islogin = false;

		} catch (NamingException e) {

			e.printStackTrace();
		} catch (Exception eee) {
			eee.printStackTrace();

		} finally {
			try {
				ctx.close();
			} catch (Exception ie) {

			}
		}
		return islogin;
	}

	@SuppressWarnings("unused")
	public static boolean userLogin(String userName, String password) { // 新建用户验证。
		Hashtable envi = new Hashtable();
		try {
			envi.put("java.naming.factory.initial",
					"com.sun.jndi.ldap.LdapCtxFactory");
			envi.put("java.naming.provider.url", "LDAP://192.168.1.199:389");
			envi.put(Context.SECURITY_AUTHENTICATION, "simple");
			envi.put("java.naming.security.principal", userName);
			envi.put("java.naming.security.credentials", password);
			InitialContext iCnt = new InitialContext(envi);
			return true;
		} catch (Exception e) {
			// e.printStackTrace();
			return false;
		} finally {
			try {
				ctx.close();
			} catch (Exception ie) {

			}
		}
	}

	@SuppressWarnings("unused")
	public static void main(String arg[]) {
		Hashtable envi = new Hashtable();
		try {
			envi.put("java.naming.factory.initial",
					"com.sun.jndi.ldap.LdapCtxFactory");
			envi.put("java.naming.provider.url", "LDAP://192.168.1.199:389");
			envi.put(Context.SECURITY_AUTHENTICATION, "simple");
			envi.put("java.naming.security.principal", "zhihong.xu@chinacache");
			envi.put("java.naming.security.credentials", "qazQAZ!2");

			InitialContext iCnt = new InitialContext(envi);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}